<?php

/**
 * 积分兑换 
 * File Name：RedeemController.php
 * File Encoding：UTF-8
 * File New Time：2014-6-14 9:35:38
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class RedeemController extends AdminBase {

    //积分配置
    public function actionConfig() {
        if (IS_POST) {
            if (empty($_POST)) {
                $this->error('配置为空！');
            }
            $model = ConfigModel::model();
            foreach ($_POST as $varname => $value) {
                $info = $model->findByPk($varname);
                if (empty($info)) {
                    continue;
                }
                $info->value = $value;
                $info->save(false);
            }
            //更新缓存
            ConfigModel::model()->getConfig(NULL);
            $this->success('修改成功！');
        } else {
            $this->assign('info', ConfigModel::model()->getConfig());
            $this->render();
        }
    }

    //商品管理
    public function actionIndex() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('index', $_POST));
            exit;
        }
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        //报名模型
        $model = RedeemModel::model();
        //条件数组
        $where = array(
            'order' => 'listorder desc,id desc'
        );
        $status = $_GET['status'];
        if ($status != '') {
            $where['status'] = (int) $status;
            $this->assign('status', $status);
        }
        //时间
        if (isset($_GET['start_time']) && $_GET['start_time']) {
            $start_time = strtotime($_GET['start_time']);
            $this->assign('start_time', $_GET['start_time']);
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
                $this->assign('end_time', $_GET['end_time']);
            }
            if ($start_time) {
                $where['start_time'] = array('EGT', $start_time);
            }
            if ($end_time > $start_time) {
                $where['start_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            }
        }
        $keyword = $_GET['keyword'];
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $where['title'] = array('LIKE', "%{$keyword}%");
        }

        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

    public function actionAdd() {
        $model = RedeemModel::model();
        if (IS_POST) {
            if ($model->add($_POST)) {
                $this->success('添加成功！', self::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '添加失败！');
            }
        } else {
            $this->render();
        }
    }

    public function actionEdit() {
        $model = RedeemModel::model();
        if (IS_POST) {
            if ($model->editor($_POST)) {
                $this->success('修改成功！', self::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $this->baseEdit($model, 'index');
        }
    }

    public function actionDelete() {
        $this->error('删除功能关闭！');
        $model = RedeemModel::model();
        $this->baseDelete($model, 'index');
    }

    //快捷编辑后，ajax重新加载内容
    public function actionPublic_load() {
        $id = Yii::app()->request->getParam('id');
        //查询报名信息
        $data = RedeemModel::model()->findByPk($id);
        if (empty($data)) {
            exit('');
        }
        $this->assign('rs', $data);
        $this->render();
    }

    //兑换记录
    public function actionRecord() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('record', $_POST));
            exit;
        }
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        //报名模型
        $model = RedeemRecordsModel::model();
        //条件数组
        $where = array(
            'order' => 'id desc',
        );
        $type = (int) $_GET['type'];
        $keyword = trim($_GET['keyword']);
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $this->assign('type', $type);
            switch ($type) {
                case 1://用户名
                    $where['username'] = $keyword;
                    break;
                case 2://用户UID
                    $where['uid'] = (int) $keyword;
                    break;
                case 3://IP地址
                    $where['ip'] = $keyword;
                    break;
            }
        }
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

}
